package com.suncaper.shop.controller;

import com.suncaper.shop.component.Utils;
import com.suncaper.shop.domain.User;
import com.suncaper.shop.mapper.UserMapper;
import com.suncaper.shop.service.EmailService;
import com.suncaper.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLOutput;
import java.util.Map;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private EmailService emailService;

    @PostMapping("/createaccount")
    public String createAccount(User user) {
        userService.save(user);
        return "redirect:/index";
    }


    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String, Object> map,
                        HttpServletResponse response,
                        HttpServletRequest request) {

        String re = null;
//        如果选择 Remember me 就记录Cookie的值
        if (request.getParameter("rememberme") != null) {
            re = request.getParameter("rememberme");
        }
        Boolean s_f = userService.login(username, password, request);
        if (s_f) {
            map.put("isLogin", true);
            if (re != null) {
                userService.rememberme(re, username, password, response);
            }
            return "redirect:/myaccount";
        } else {
            map.put("msg", "用户名密码错误");
            return "create_account";
        }
    }


    @RequestMapping("/logout")
    public String logout(HttpSession session, Map<String, Object> map, HttpServletRequest httpRequest) {
        userService.logout(session, map);
        userService.getCookie(httpRequest, map);
        return "create_account";
    }


    @PostMapping("/updateUser")
    public String register(User user,
                           @RequestParam("password") String password,
                           @RequestParam("password1") String password1,
                           @RequestParam("password2") String password2,
                           Map<String, Object> map,
                           HttpSession session) {

        if (userService.updateById(user, map, password, password1, password2, session)) {
            return "my_account";
        }
        return "account_details";
    }

    @RequestMapping(value = {"/lost_password", "/lost_password.html"})
    public String lostPassword() {
        return "lost_password";
    }

    @PostMapping("/resetpassword")
    public String restPassword(@RequestParam(value = "email", required = false) String email,
                               @RequestParam(value = "code", required = false) String code,
                               HttpSession session,
                               Map map) {


        return userService.restPassword(email,code,session,map);
    }


    @RequestMapping("/saler_logout")
    public String saler_logout(HttpSession session, Map<String, Object> map, HttpServletRequest httpRequest) {
        userService.logout(session, map);
        userService.getCookie(httpRequest, map);
        return "saler_login";
    }

    @PostMapping("/register")
    public String register(@RequestParam("check") String check,
                           @RequestParam("pid") Integer pid,
                           HttpSession session) {

        userService.register(check, pid, session);
        return "redirect:/myaccount";
    }

    @PostMapping("/saler/login")
    public String salerlogin(@RequestParam("username") String username,
                             @RequestParam("password") String password,
                             Map<String, Object> map,
                             HttpServletResponse response,
                             HttpServletRequest request) {

        return userService.salerLogin(request, username, password, map, response);
    }
}
